package me.hushuang.webb.back.service;

import me.hushuang.webb.back.mapper.UserMapper;
import me.hushuang.webb.back.model.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.List;
import java.util.Set;

/**
 * 用户接口
 */
@Service
public class UserService {

    @Autowired
    private UserMapper userMapper;
    @Autowired
    private PasswordHelper passwordHelper;
    @Autowired
    private RoleService roleService;

    /**
     * 创建用户
     * @param user
     */
    public int createUser(User user) {
        //加密密码
        passwordHelper.encryptPassword(user);
        return userMapper.createUser(user);
    }

    public void updateUser(User user) {
        userMapper.updateUser(user);
    }

    public void deleteUser(Long userId) {
        userMapper.deleteUser(userId);
    }

    /**
     * 修改密码
     * @param userId
     * @param newPassword
     */
    public void changePassword(Long userId, String newPassword) {
        User user =userMapper.findOne(userId);
        user.setPassword(newPassword);
        passwordHelper.encryptPassword(user);
        userMapper.updateUser(user);
    }

    public User findOne(Long userId) {
        return userMapper.findOne(userId);
    }

    public List<User> findAll() {
//        User user = new User();
//        user.setLocked(false);
//        user.setUsername("abcdefgh");
//        user.setPassword("123");
//        passwordHelper.encryptPassword(user);
//        userMapper.createUser(user);
//        getname();

        return userMapper.findAll();
    }

    /**
     * 根据用户名查找用户
     * @param username
     * @return
     */
    public User findByUsername(String username) {
        return userMapper.findByUsername(username);
    }

    /**
     * 根据用户名查找其角色
     * @param username
     * @return
     */
    public Set<String> findRoles(String username) {
        User user =findByUsername(username);
        if(user == null) {
            return Collections.EMPTY_SET;
        }
        return roleService.findRoles(user.getRoleIds().toArray(new Long[0]));
    }

    /**
     * 根据用户名查找其权限
     * @param username
     * @return
     */
    public Set<String> findPermissions(String username) {
        User user =findByUsername(username);
        if(user == null) {
            return Collections.EMPTY_SET;
        }
        return roleService.findPermissions(user.getRoleIds().toArray(new Long[0]));
    }

    public int getname() {
        int a = 15;
        int b = 0;
        int result;
        result = a/b;
        return result;
    }
//        public static void main (String args[]) {
//            getname();
////            int array[]={20,20,40};
////            int num1=15,num2=0;
////            int result=0;
////            try {
////                result = num1/num2;
////                System.out.println("The result is" +result);
////                for(int i = 2;i >=0; i--){
////                    System.out.println
////                            ("The value of array is" +array[i]);
////                }
////            }
////            catch (ArrayIndexOutOfBoundsException e) {
////                System.out.println
////                        ("Error. Array is out of Bounds"+e);
////            }
////            catch (ArithmeticException e) {
////                System.out.println
////                        ("Can't be divided by Zero"+e);
////            }
//        }


}
